/// @Classname : HcDialog
/// @Author :
/// @Time : 2024-03-06 15:40
/// @Description : //todo

part of hc_dialog_util;

class _HcDialog extends AlertDialog {
  final Widget? titleWidget;
  final Widget? contentWidget;
  final Widget? warningWidget;
  final HcDialogTheme? themeData;
  final List<String>? buttonTextList;
  final List<Widget>? buttonList;
  final Widget? iconWidget;
  final int titleMaxLines;
  final double? minHeight;
  final double? maxHeight;
  final DialogIndexFunction? indexFunction;
  final HcButtonType mainButtonType;
  final HcButtonType secondaryButtonType;
  final bool buttonGroupReverse;
  final Axis buttonDecoration;
  final bool autoClose;
  final double? buttonRadius;
  final Color? mainButtonBackground;
  final Color? secondaryButtonBackground;
  final bool showCloseIcon;
  final Widget? closeIcon;
  final bool useNavigateBack;

  ///  构建弹窗
  ///  [iconWidget] 展示图标组件
  ///  [titleWidget] 文字标题
  ///  [contentWidget] 副标题文字
  ///  [warningWidget] 警告文字
  ///  [titleMaxLines] 标题最大显示行数
  ///  [minHeight] 最小展示高度
  ///  [maxHeight] 最大展示高度
  ///  [indexFunction]按钮点击的回调
  ///  [mainButtonType]按钮的样式
  ///  [buttonGroupReverse]主副按钮位置是否对调
  ///  [buttonDecoration]按钮排列方向
  ///  [autoClose]是否自动关闭
  const _HcDialog({
    this.iconWidget,
    this.buttonTextList,
    this.mainButtonType = HcButtonType.elevated,
    this.mainButtonBackground,
    this.buttonList,
    this.titleWidget,
    this.contentWidget,
    this.warningWidget,
    this.themeData,
    this.titleMaxLines = 3,
    this.minHeight,
    this.maxHeight,
    this.indexFunction,
    this.secondaryButtonType = HcButtonType.outlined,
    this.secondaryButtonBackground,
    this.buttonGroupReverse = false,
    this.buttonDecoration = Axis.horizontal,
    this.autoClose = true,
    this.buttonRadius,
    this.showCloseIcon = true,
    this.useNavigateBack = true,
    this.closeIcon,
  });

  @override
  Widget build(BuildContext context) {
    HcDialogTheme theme = themeData ?? HcDialogTheme();
    theme = HcCustomerConfig().allTheme.dialogTheme.merge(theme);

    //最小高度
    double finalMinHeight =
        minHeight ?? MediaQuery.of(context).size.height * 0.2;
    //最大高度
    double finalMaxHeight = max(
        finalMinHeight, maxHeight ?? MediaQuery.of(context).size.height * 0.7);
    //最终的对齐方向
    Axis finalDecoration =
        (buttonTextList != null && buttonTextList!.length > 2 ||
                buttonList != null && buttonList!.length > 2)
            ? Axis.vertical
            : buttonDecoration;

    //构建消息显示
    Widget buildContentWidget() {
      if (contentWidget != null) return contentWidget!;
      return const SizedBox.shrink();
    }

    bool isOnlyIcon =
        titleWidget == null && contentWidget == null && warningWidget == null;

    final List<Widget> children = <Widget>[
      Padding(
        padding: theme.widgetPadding,
        child: Column(
          children: [
            if (iconWidget != null)
              Container(
                margin: isOnlyIcon ? EdgeInsets.zero : theme.iconMargin,
                child: IconTheme(
                    data: IconThemeData(
                        color: theme.iconColor, size: theme.iconSize),
                    child: iconWidget!),
              ),
            if (titleWidget != null)
              Container(
                margin: contentWidget != null ? theme.titleMargin : null,
                padding: theme.titlePadding,
                child: DefaultTextStyle(
                  style: theme.titleTextStyle,
                  maxLines: titleMaxLines,
                  textAlign: theme.titleTextAlign,
                  overflow: TextOverflow.ellipsis,
                  child: titleWidget!,
                ),
              ),
            Container(
              padding: theme.contentPadding,
              constraints: BoxConstraints(
                  minHeight: finalMinHeight, maxHeight: finalMaxHeight),
              child: ScrollConfiguration(
                behavior: const ScrollBehavior(),
                child: SingleChildScrollView(
                    child: Container(
                  constraints: BoxConstraints(
                    minHeight: finalMinHeight,
                  ),
                  child: Center(
                    child: DefaultTextStyle(
                      style: theme.contentTextStyle,
                      textAlign: theme.contentTextAlign,
                      child: buildContentWidget(),
                    ),
                  ),
                )),
              ),
            ),
            if (warningWidget != null)
              Container(
                margin: theme.warningMargin,
                padding: theme.warningPadding,
                child: DefaultTextStyle(
                  style: theme.warningTextStyle,
                  textAlign: theme.warningTextAlign,
                  child: warningWidget!,
                ),
              ),
          ],
        ),
      ),
      if (buttonList != null)
        Padding(
          padding: theme.buttonListPadding,
          child: ClipRRect(
            borderRadius: theme.buttonListPadding.left == 0 &&
                    theme.buttonListPadding.right == 0
                ? BorderRadius.only(
                    bottomRight: Radius.circular(buttonRadius ?? theme.radius),
                    bottomLeft: Radius.circular(buttonRadius ?? theme.radius))
                : BorderRadius.zero,
            child: HcGrid(
                crossAxisSpacing: theme.buttonSpacing,
                mainAxisSpacing: theme.buttonSpacing,
                itemBuilder: (itemContext, index) {
                  return Ink(
                    child: InkWell(
                      onTap: () => {
                        if (indexFunction != null) {indexFunction!.call(index)},
                        if (autoClose) {Navigator.pop(context)}
                      },
                      child: AbsorbPointer(
                        absorbing: true,
                        child: SizedBox(
                          width: double.infinity,
                          child: buttonList![index],
                        ),
                      ),
                    ),
                  );
                },
                crossAxisCount:
                    finalDecoration == Axis.vertical ? 1 : buttonList!.length,
                itemCount: buttonList!.length),
          ),
        )
      else if (buttonTextList != null)
        HcDialogButtonPanel(
            mainButtonText: buttonTextList!.removeAt(0),
            decoration: buttonDecoration,
            buttonGroupReverse: buttonGroupReverse,
            mainButtonType: mainButtonType,
            secondaryButtonType: secondaryButtonType,
            radius: buttonRadius ?? theme.radius,
            padding: theme.buttonListPadding,
            borderColor: theme.borderColor,
            borderWidth: theme.borderWidth,
            secondaryButtonList: buttonTextList!
                .map((e) => HcButtonPanelConfig(label: e))
                .toList(),
            mainButtonOnTap: () {
              if (indexFunction != null) {
                indexFunction!.call(0);
              }
              if (autoClose) {
                Navigator.pop(context);
              }
            },
            secondaryButtonOnTap: (int index) {
              if (indexFunction != null) {
                indexFunction!.call(index);
              }
              if (autoClose) {
                Navigator.pop(context);
              }
            },
            mainButtonDisabled: false,
            buttonSpacing: theme.buttonSpacing),
    ];

    Widget buildContainer() {
      return Stack(
        children: [
          if (showCloseIcon)
            Positioned(
              right: 0,
              top: 0,
              child: HcTextButton.icon(
                onPressed: () {
                  Navigator.pop(context);
                },
                child: closeIcon ?? const Icon(Icons.close),
              ),
            ),
          Column(
            mainAxisSize: MainAxisSize.max,
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: children,
          )
        ],
      );
    }

    return PopScope(
      canPop: useNavigateBack,
      //抵消Dialog对宽度的限制
      child: UnconstrainedBox(
        //自定义宽度
        child: SizedBox(
          width: theme.width,
          child: Material(
            color: theme.background,
            borderRadius: BorderRadius.all(Radius.circular(theme.radius)),
            child: buildContainer(),
          ),
        ),
      ),
    );
  }
}
